﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class SparePartApplySendProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        private void InitialPage()
        {
            SparePartApplyService service = new SparePartApplyService();
            SparePartApplyForm sparePartApplyForm = service.GetProductDetailForm(long.Parse(Request.QueryString[Constants.QUERYSTR_SPAREPARTAPPLY_ID]));
            serviceStationLabel.Text = sparePartApplyForm.ServiceStation.Name;
            productTypeLabel.Text = sparePartApplyForm.ApplicationProduct.ProductType.ProductTypeName;
            countLabel.Text = sparePartApplyForm.ApplicationProduct.ProductCount.ToString();
            sendPlaceTextBox.Text = sparePartApplyForm.ServiceStation.Address;
            dealDateLabel.Text = sparePartApplyForm.DealWithTime.HasValue?sparePartApplyForm.DealWithTime.Value.ToString("yyyy-MM-dd"):"";
            sendDateLabel.Text = DateTime.Now.ToString("yyyy-MM-dd");
            RMALabel.Text = sparePartApplyForm.ApplicationProduct.ApplicationForm.ApplicationCode;
        }

        // 点击通知的事件处理程序，打开邮件发送对话框
        protected void mailLinkButton_Click(object sender, EventArgs e)
        {
            mailModalPopupExtender.Show();
            SparePartApplyService service = new SparePartApplyService();
            SparePartApplyForm sparePartApplyForm = service.GetSparePartFormApplicant(long.Parse(Request.QueryString[Constants.QUERYSTR_SPAREPARTAPPLY_ID]));
            if (!sparePartApplyForm.ApplicationProductReference.IsLoaded)
            {
                sparePartApplyForm.ApplicationProductReference.Load();
            }
            if (!sparePartApplyForm.ApplicationProduct.ProductTypeReference.IsLoaded)
            {
                sparePartApplyForm.ApplicationProduct.ProductTypeReference.Load();
            }
            if (!sparePartApplyForm.UserReference.IsLoaded)
            {
                sparePartApplyForm.UserReference.Load();
            }
            if (!sparePartApplyForm.User.ServiceStationReference.IsLoaded)
            {
                sparePartApplyForm.User.ServiceStationReference.Load();
            }

            receivePersonLabel.Text = sparePartApplyForm.User.UserEmail;
            connectPersonLabel.Text = sparePartApplyForm.User.ContactPerson;
            string mailContent = System.Configuration.ConfigurationManager.AppSettings["SparePartSendProduct"];
            mailTextBox.Text = string.Format(mailContent, sparePartApplyForm.User.ContactPerson, RMALabel.Text, DateTime.Now.ToString("yyyy-MM-dd"), sparePartApplyForm.ApplicationProduct.ProductType.ProductTypeName, sparePartApplyForm.User.ServiceStation.Address);
        }
        // 点击邮件发送对话框上的发送按钮的事件处理程序
        protected void sendLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                SparePartApplyService service = new SparePartApplyService();
                SparePartApplyForm form = service.GetSparePartApplyFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_SPAREPARTAPPLY_ID]));
                if (!form.UserReference.IsLoaded)
                {
                    form.UserReference.Load();
                }
                EmailService.SendEmail(form.User.UserEmail, "备件已发货", mailTextBox.Text);
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "SparePartApplySendProduct.aspx::sendLinkButton_Click");
                throw ex;
            }
        }

        protected void saveLinkButton_Click(object sender, EventArgs e)
        {
            try
            {
                SparePartApplyService service = new SparePartApplyService();
                SparePartApplyForm form = service.GetSparePartApplyFormByID(long.Parse(Request.QueryString[Constants.QUERYSTR_SPAREPARTAPPLY_ID]));
                form.SendTime = DateTime.Now;
                form.SendProductSerialNumber = serialNumberTextBox.Text;
                form.SendPlace = sendPlaceTextBox.Text;
                form.Status = "备件-待收货确认";
                service.Update(form);

                // 增加出货记录
                Response.Write(string.Format("<script language='JavaScript'> alert( '发货成功');location.href= ' SpartPartApplyListSend.aspx' </script>"));
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "SparePartApplySendProduct.aspx::saveLinkButton_Click");
                throw ex;
            }
        }        
    }
}
